/**
 * Created by tuyan on 2016/11/29.
 */

jQuery.support.cors = true;
$.ajaxSetup({
    dataType: 'json',
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    }
});

window.Cherry.http = {};
$.extend(window.Cherry.http, {
    /**
     * Ajax请求返回成功编码.
     */
    AJAX_RESPONSE_SUCCESS: "00000",

    CONFIRM_TITLE: '确认执行此操作？',

    /**
     * http的post请求
     */
    HTTP_POST: 'POST',

    /**
     * http的get请求
     */
    HTTP_GET: 'GET',

    /**
     * Ajax提交表单.
     *
     * @param _formId           要提交的表单元素ID
     * @param _submitUrl        要提交数据的地址
     * @param _validators       表单验证器
     * @param _successFunc      提交操作成功回调函数
     * @param _errorFunc        提交操作失败回调函数
     */
    submitForm: function(_formId, _submitUrl, _validators, _successFunc, _errorFunc) {
        var that = this;
        var $form = $("#" + _formId);
        if ($form) {
            if(_validators){
                $form.validate($.extend({
                    ignore: "",
                    submitHandler: function(form) {
                        //验证成功提交表单
                        // Ajax提交
                        var formData = that.serializeFormData(_formId);
                        if (formData) {
                            // submitUrl, "正在操作中，请稍后...", formData, _successFunc, _errorFunc
                            that.ajaxRequest({
                                url: _submitUrl,
                                method: Cherry.http.HTTP_POST,
                                data: formData,
                                success: _successFunc,
                                error: _errorFunc
                            });
                        } else {
                            Cherry.errorTip("提交数据错误！");
                        }
                    }
                },_validators));
                $form.on("submit", function(event){
                    // 停止表单提交事件传递，不执行直接提交操作
                    event.preventDefault();
                });
                $form.submit();
            }else {
                Cherry.errorTip("找不到表单验证器！");
            }
        } else {
            Cherry.errorTip("找不到ID为" + _formId + "的表单！");
        }
    },
    /**
     * 序列化表单数据.
     *
     * @param _formId   要序列化的表单元素ID
     * @returns {*}
     */
    serializeFormData: function(_formId) {
        var $form = $("#" + _formId);
        if ($form) {
            var dataJson = $form.serializeArray();
            var data = {};
            if (dataJson.length > 0) {
                for (var i in dataJson) {
                    if (data[dataJson[i].name]) {
                        data[dataJson[i].name] += "," + dataJson[i].value;
                    } else {
                        data[dataJson[i].name] = dataJson[i].value;
                    }
                }
            }
            return data;
        } else {
            Cherry.errorTip("无法序列化表单数据！");
            return undefined;
        }
    },
    /**
     * 发送一个Ajax请求
     *
     * @param _options
     *  url               请求地址
     *  method            请求方法类型：POST，GET
     *  loadingTip       加载提示的信息内容
     *  data              请求发送的数据
     *  success      请求后台返回成功之后的回调函数
     *  error        请求后台返回失败之后的回调函数
     */
    ajaxRequest: function(_options) {
        _options = $.extend({
            url: '',
            method: 'GET',
            loadingTip: '正在操作中，请稍后...',
            data: {},
            traditional: false,
            success: function(data){
                Cherry.successTip("操作成功！");
            },
            error: function(){
                Cherry.errorTip("操作失败！");
            }
        },_options);
        var _loadingId;
        $.ajax({
            url: _options.url,
            method: _options.method,
            cache: false,
            dataType: 'json',
            data: _options.data,
            traditional: _options.traditional,
            xhrFields: {
                withCredentials: true
            },
            beforeSend: function(xhr) {
                _loadingId = Cherry.loadingTip(_options.loadingTip);
                return true;
            },
            success: function(response) {
                layer.close(_loadingId);
                if (response.data == 'null' || response.data == null) {
                    response.data = undefined;
                }
                if (response.code == Cherry.http.AJAX_RESPONSE_SUCCESS) {
                    // 后台返回成功操作
                    if (_options.success) {
                        _options.success(response.data);
                    }
                } else {
                    // 先判断登录是否超时
                    if (response.isSessionTimeout) {
                        // FIXME 登录超时操作
                    } else if (!response.authorized) {
                        // 再判断是否有权限
                        Cherry.errorTip("您没有访问权限！");
                    } else {
                        // 后台返回错误操作
                        if (_options.error) {
                            _options.error(response);
                        } else {
                            Cherry.errorTip("操作错误，请联系管理员！");
                        }
                    }
                }
            },
            error: function(errorResponse) {
                layer.close(_loadingId);
                Cherry.errorTip("未知问题，请联系管理员！");
            }
        });
    },

    /**
     * Ajax加载数据.
     *
     * @param _url              加载数据的地址
     * @param _data             请求参数对象
     * @param _successFunc      加载成功回调函数
     * @param _errorFunc        记载失败回调函数
     */
    getData: function(_url, _data, _successFunc, _errorFunc){
        this.ajaxRequest({
            url: _url,
            method: Cherry.http.HTTP_GET,
            data: _data,
            success: _successFunc,
            error: _errorFunc
        });
    },

    /**
     * Ajax删除数据.
     *
     * @param _url              删除数据的地址
     * @param _data             请求参数对象
     * @param _successFunc      删除成功回调函数
     * @param _errorFunc        删除失败回调函数
     */
    deleteData: function(_url, _data, _successFunc, _errorFunc){
        var that = this;
        Cherry.confirm('确认删除此行数据？',function(){
            that.ajaxRequest({
                url: _url,
                method: Cherry.http.HTTP_POST,
                data: _data,
                success: _successFunc,
                error: _errorFunc
            });
        });
    },

    /**
     * Ajax操作数据.
     *
     * @param _url              数据的地址
     * @param _data             请求参数对象
     * @param _successFunc      成功回调函数
     * @param _errorFunc        失败回调函数
     */
    operateData: function (_url, _data, _successFunc, _errorFunc){
        var that = this;
        Cherry.confirm(that.CONFIRM_TITLE,function(){
            that.ajaxRequest({
                url: _url,
                method: Cherry.http.HTTP_POST,
                data: _data,
                success: _successFunc,
                error: _errorFunc
            });
        });
    },

    /**
     * 动态加载单选下拉列表
     * @param _selectId                     下拉列表对象ID
     * @param _url                          下拉信息请求路径
     * @param _keyName                       下拉value属性名称
     * @param _valueName                     下拉显示text属性名称
     * @param _defSelectVal                 默认选中值
     */
    comboBox: function(_selectId, _url,_data, _keyName, _valueName, _defSelectVal){
        var that = this;
        that.ajaxRequest({
            url: _url,
            method: Cherry.http.HTTP_GET,
            data: _data,
            success: function(responseData){
                var $select = $("#"+_selectId);
                if (responseData && responseData.length>0){
                    $.each(responseData, function(i, n){
                        var _option = $("<option></option>");
                        _option.val(n[_keyName]);
                        _option.text(n[_valueName]);
                        if(_defSelectVal){
                            if(n[_keyName] == _defSelectVal){
                                _option.attr("selected", "selected");
                            }
                        }
                        $select.append(_option);
                    });
                    $select.selectpicker({
                        iconBase: 'fa',
                        tickIcon: 'fa-check'
                    });
                    $select.selectpicker('refresh');
                }else {
                    $select.html($("<option></option>").val("").text("请选择"));
                    $select.selectpicker('refresh');
                }
            },
            error: function(){
                Cherry.errorTip("数据加载失败！");
            }
        });
    },
    /**
     * 动态加载单选下拉列表
     * @param _selectId                     下拉列表对象ID
     * @param _url                          下拉信息请求路径
     * @param _keyName                       下拉value属性名称
     * @param _valueName                     下拉显示text属性名称
     * @param _defSelectVal                 默认选中值
     */
    comBox: function(_selectId, _url,_data, _keyName, _valueName, _defSelectVal){
        var that = this;
        that.ajaxRequest({
            url: _url,
            method: Cherry.http.HTTP_GET,
            data: _data,
            success: function(responseData){
                var $select = $("#"+_selectId);
                responseData = responseData.rows;
                if (responseData && responseData.length>0){
                    $.each(responseData, function(i, n){
                        var _option = $("<option></option>");
                        _option.val(n[_keyName]);
                        _option.text(n[_valueName]);
                        if(_defSelectVal){
                            if(n[_keyName] == _defSelectVal){
                                _option.attr("selected", "selected");
                            }
                        }
                        $select.append(_option);
                    });
                    $select.selectpicker({
                        iconBase: 'fa',
                        tickIcon: 'fa-check'
                    });
                    $select.selectpicker('refresh');
                }else {
                    $select.html($("<option></option>").val("").text("请选择"));
                    $select.selectpicker('refresh');
                }
               /* var rows = responseData.rows;
                rows = eval(rows);
                if (rows.length>0){
                    $.each(rows, function(i, n){
                        var _option = "";
                        if(_defSelectVal){
                            if(n[_keyName] == _defSelectVal){
                                _option = "<option selected value='"+n[_valueName]+"'>"+n[_valueName]+"</option>";
                            }
                        }else{
                            _option = "<option value='"+n[_valueName]+"'>"+n[_valueName]+"</option>";
                        }
                        $select.append(_option);
                    });
                    $select.selectpicker({
                        iconBase: 'fa',
                        tickIcon: 'fa-check'
                    });
                    $select.selectpicker('refresh');
                }else {
                    $select.html($("<option></option>").val("").text("请选择"));
                    $select.selectpicker('refresh');
                }*/
            },
            error: function(){
                Cherry.errorTip("数据加载失败！");
            }
        });
    },
    /**
     * 动态加载单选下拉列表(带括号提示)
     * selectId                     下拉列表对象ID
     * url                          下拉信息请求路径
     * keyName                      下拉value属性名称
     * valueName                    下拉显示text属性名称
     * pointName                    括号提示信息属性名称(若没有，传false；否则，传取值属性名称)
     * dictName                     数据字典值,typeCode(若没有，传false；否则，传数据字典typeCode)
     * defSelectVal                 默认选中值[*]
     */
    comboBox2: function(_options){
        var that = this;
        var defOptions = {
            selectId: '',
            url: '',
            data: {},
            keyName: 'id',
            valueName: '',
            pointName: undefined,
            dictName: undefined,
            customName: undefined,
            defSelectVal: undefined,
            callBackFunc: undefined
        };
        _options = $.extend(defOptions, _options);
        that.ajaxRequest({
            url: _options.url,
            method: Cherry.http.HTTP_GET,
            data: _options.data,
            success: function(responseData){
                var $select = $("#"+_options.selectId);
                if (responseData && responseData.length>0){
                    $.each(responseData, function(i, n){
                        var _option = $("<option></option>"), pointName = '';
                        _option.val(n[_options.keyName]);
                        if (_options.pointName){
                            pointName = _options.dictName ? Cherry.dict.dictFormat(_options.dictName, n[_options.pointName]) : n[_options.pointName];
                            pointName = ' ('+pointName+')';
                        }
                        _option.text(n[_options.valueName]+pointName);
                        if (_options.customName){
                            _option.attr('custom-text', n[_options.customName]);
                        }
                        $select.append(_option);
                    });
                    //是否有回调函数
                    if (_options.callBackFunc){
                        $select.off('change').on('change', function(){
                            var val = $(this).val();
                            if(val != undefined && val.length == 1){
                                val = $(this).children('option:selected').val();
                            }
                            var text = $(this).children('option:selected').text();
                            var customText = $(this).children('option:selected').attr('custom-text');
                            console.log(val);
                            _options.callBackFunc(val, text, customText);
                        });
                    }
                }
                $select.selectpicker({
                    iconBase: 'fa',
                    tickIcon: 'fa-check'
                });
                if(_options.defSelectVal != undefined && _options.defSelectVal.length > 0){
                    $select.selectpicker('val', _options.defSelectVal);//默认选中
                }
                $select.selectpicker('refresh');
            },
            error: function(){
                Cherry.errorTip("数据加载失败！");
            }
        });
    },

    /**
     * 动态加载多选下拉列表
     * @param _selectId                     下拉列表对象ID
     * @param _url                          下拉信息请求路径
     * @param keyName                       下拉value属性名称
     * @param valueName                     下拉显示text属性名称
     * @param _defSelectVals                默认选中值数组（[]）
     */
    comboBoxs: function(_selectId, _url,_data, keyName, valueName, _defSelectVals){
        var that = this;
        that.ajaxRequest({
            url: _url,
            method: Cherry.http.HTTP_GET,
            data: _data,
            success: function(responseData){
                if (responseData && responseData.length>0){
                    var $select = $("#"+_selectId);
                    $select.empty();
                    $.each(responseData, function(i, n){
                        var _option = $("<option></option>");
                        _option.val(n[keyName]);
                        _option.text(n[valueName]);
                        if(_defSelectVals){
                            if($.isArray(_defSelectVals)){
                                $.each(_defSelectVals, function(index, obj){
                                    if(n[keyName] == obj){
                                        _option.attr("selected", "selected");
                                    }
                                });
                            }
                        }
                        $select.append(_option);
                    });
                    $select.selectpicker({
                        iconBase: 'fa',
                        tickIcon: 'fa-check'
                    });
                    $select.selectpicker('refresh');
                }else {
                    $("#"+_selectId).html("");
                    $("#"+_selectId).selectpicker('refresh');
                }
            },
            error: function(){
                Cherry.errorTip("数据加载失败！");
            }
        });
    },


    /**
     * 加载树结构
     * @param _url              加载数据url
     * @param _data             加载数据参数
     * @param _treeViewId       加载树容器ID
     * @param _idKey            id属性
     * @param _textKey          text属性
     * @param _iconKey          icon属性
     * @param _listKey          children属性
     * @param _callBackFunc     树节点点击事件回调函数
     * @param _selectNodeId     默认选中节点ID
     */
    /*loadTree: function(_url,_data,_treeViewId,_idKey, _textKey, _iconKey, _listKey,_callBackFunc){
        var that = this;
        that.getData(_url,_data,function(responseData){
            console.log(responseData);
            if(responseData) {
                var treeData = [], $treeView = $("#"+_treeViewId);
                that.listTreeData(responseData,treeData,_idKey, _textKey, _iconKey, _listKey);
                if(treeData){
                    $treeView.jstree({
                        'core' : {
                            multiple : false,
                            data: treeData
                        }
                    }).on('select_node.jstree',function(e,data){
                        //当前选中节点的id
                        var nodeId = data.instance.get_node(data.selected[0]).id;
                        var nodeText = data.instance.get_node(data.selected[0]).text;
                        if(_callBackFunc){
                            _callBackFunc(nodeId, nodeText);
                        }
                    }).on('loaded.jstree', function(e, data){
                        var inst = data.instance;
                        var obj = inst.get_node(e.target.firstChild.firstChild.lastChild);
                        inst.open_node(obj);
                        inst.select_node(obj);
                    });
                }
            }
        });
    },*/
    loadTree: function(_url,_data,_treeViewId,_idKey, _textKey, _iconKey, _listKey,_callBackFunc, _selectNodeId){
        var that = this;
        var treeData = [], $treeView = $("#"+_treeViewId);
        $treeView.jstree({
            'core' : {
                multiple : false,
                data: {
                    url:_url,
                    dataFilter:function(data){
                        var json = JSON.parse(data);
                        that.listTreeData(json.data,treeData,_idKey, _textKey, _iconKey, _listKey);
                        return JSON.stringify(treeData);
                    }
                },
                strings: {
                    'Loading ...' : '正在加载...'
                }
            }
        });

        $treeView.off('select_node.jstree').on('select_node.jstree',function(e,data){
            //当前选中节点的id
            var nodeId = data.instance.get_node(data.selected[0]).id;
            var nodeText = data.instance.get_node(data.selected[0]).text;
            if(_callBackFunc){
                _callBackFunc(nodeId, nodeText);
            }
        }).off('loaded.jstree').on('loaded.jstree', function(e, data){
            var inst = data.instance;
            if(_selectNodeId){
                var node = $treeView.jstree('get_node', _selectNodeId);
                //$treeView.jstree('deselect_node', node.parents);
                $treeView.jstree('select_node', _selectNodeId);
                $treeView.jstree('open_node', _selectNodeId);
            }else {
                var obj = inst.get_node(e.target.firstChild.firstChild.lastChild);
                inst.open_node(obj);
                inst.select_node(obj);
            }
        });
    },

    /**
     * 清除树
     */
    cleanTree: function(_treeViewId){
        //这个是关键，如果不清空实例，jstree不会重新生成
        $('#'+_treeViewId).data('jstree', false).empty();
    },

    /**
     * 将请求原始数据转化为jstree显示的数据结构
     *
     * @param data              请求返回数据结构
     * @param treeData          显示树数据结构
     * @param _idKey            id属性
     * @param _textKey          text属性
     * @param _iconKey          icon属性
     * @param _listKey          children属性
     */
    listTreeData: function(data, treeData, _idKey, _textKey, _iconKey, _listKey) {
        var that = this;
        $.each(data, function(i,n){
            var obj = new Object();
            obj.id = n[_idKey];
            obj.text = n[_textKey];
            obj.icon = n[_iconKey] ? 'fa '+n[_iconKey] : 'fa fa-file-text-o';
            if(n[_listKey]){
                obj.children = [];
                that.listTreeData(n[_listKey],obj.children,_idKey, _textKey, _iconKey, _listKey);
            }
            treeData.push(obj);
        });
    },

    /**
     * 将请求原始数据转化为jstree显示的数据结构
     *
     * @param data              请求返回数据结构
     * @param treeData          显示树数据结构
     * @param _idKey            id属性
     * @param _textKey          text属性
     * @param _iconKey          icon属性
     * @param _listKey          children属性
     */
    listTreeData2: function(data, treeData, _idKey, _textKey, _iconKey, _listKey, _id2Key) {
        var that = this;
        $.each(data, function(i,n){
            var obj = new Object();
            obj.id = n[_idKey]+','+n[_id2Key];
            obj.text = n[_textKey];
            obj.icon = n[_iconKey] ? 'fa '+n[_iconKey] : 'fa fa-file-text-o';
            if(n[_listKey]){
                obj.children = [];
                that.listTreeData(n[_listKey],obj.children,_idKey, _textKey, _iconKey, _listKey,_id2Key);
            }
            treeData.push(obj);
        });
    },

    /**
     * 联动下拉框默认参数
     */
    gangedComboxBoxOption: {
        //第一级
        selectId: 'proName',                                    //下拉框ID
        url: Cherry.dataRequestURI.selectProvince,                //请求URL
        data: {},                                               //请求参数
        keyName: 'regionCode',                                  //下拉value属性名称
        valueName: 'regionName',                                //下拉显示text属性名称
        defSelectVal: undefined,                                //默认选中值数组
        callbacksFunc: undefined,
        //第二级
        childSelectId: 'cityName',                              //下拉框ID
        childUrl: Cherry.dataRequestURI.selectCity,         //请求URL
        childData: {},                                          //请求参数
        propertyName: 'regionCode',                       //联动查询参数名称
        childKeyName: 'regionCode',                             //下拉value属性名称
        childValueName: 'regionName',                           //下拉显示text属性名称
        childDefSelectVal: undefined,                           //默认选中值数组
        childCallbacksFunc: undefined
    },

    /**
     * 级联下拉框
     * @param _options  参数对象
     */
    gangedComboxBox: function (_options) {
        var that = this;
        _options = $.extend(that.gangedComboxBoxOption, _options);
        var $selectId = $("#"+_options.selectId), $childSelectId = $("#"+_options.childSelectId), _formData = {};
        //第一级下拉change事件绑定
        $selectId.on('change',function(){
            var val = $.trim($(this).children('option:selected').val());
            val = val === 0 ? val+'' : val;
            var text = $.trim($(this).children('option:selected').text());
            if(val && val.length>0){
                _formData[_options.propertyName] = val;
                var _data = $.extend(_options.childData, _formData);
                //console.log(_data);
                $childSelectId.html($("<option></option>").val("").text("请选择"));
                that.comboBox(_options.childSelectId, _options.childUrl, _data, _options.childKeyName, _options.childValueName, _options.childDefSelectVal);
            }else {
                $childSelectId.html($("<option></option>").val("").text("请选择"));
                $childSelectId.selectpicker('refresh');
            }
            if(_options.callbacksFunc){
                _options.callbacksFunc(val, text);
            }
        });
        that.comboBox(_options.selectId, _options.url, _options.data, _options.keyName, _options.valueName, _options.defSelectVal);
        if(_options.defSelectVal && _options.childDefSelectVal){
            _formData[_options.propertyName] = _options.defSelectVal;
            var _data = $.extend(_options.childData, _formData);
            that.comboBox(_options.childSelectId, _options.childUrl, _data, _options.childKeyName, _options.childValueName, _options.childDefSelectVal);
        }
        //第二级下拉change事件绑定
        $childSelectId.on('change',function(){
            var val = $.trim($(this).children('option:selected').val());
            var text = $.trim($(this).children('option:selected').text());
            if(_options.childCallbacksFunc){
                _options.childCallbacksFunc(val, text);
            }
        });
    },

    /**
     * 级联下拉框 三级联动
     * @param _options  参数对象
     */
    gangedComboxBoxs: function (_options) {
        var that = this,
        //级联下拉框默认参数
        gangedComboxBoxOption = {
            //第一级
            selectId: 'proName',                                    //下拉框ID
            url: undefined,                                         //请求URL
            data: {},                                               //请求参数
            keyName: 'regionCode',                                  //下拉value属性名称
            valueName: 'regionName',                                //下拉显示text属性名称
            defSelectVal: undefined,                                //默认选中值数组
            callbacksFunc: undefined,
            //第二级
            childSelectId: 'cityName',                              //下拉框ID
            childUrl: undefined,                                    //请求URL
            childData: {},                                          //请求参数
            propertyName: 'provinceCode',                             //联动查询参数名称
            childKeyName: 'regionCode',                             //下拉value属性名称
            childValueName: 'regionName',                           //下拉显示text属性名称
            childDefSelectVal: undefined,                           //默认选中值数组
            childCallbacksFunc: undefined,
            //第三级
            threeSelectId: undefined,                               //下拉框ID
            threeUrl: undefined,                                    //请求URL
            threeData: {},                                          //请求参数
            threeName: 'cityCode',                                //联动查询参数名称
            threeKeyName: 'regionCode',                             //下拉value属性名称
            threeValueName: 'regionName',                           //下拉显示text属性名称
            threeDefSelectVal: undefined,                           //默认选中值数组
            threeCallbacksFunc: undefined
        };
        _options = $.extend(gangedComboxBoxOption, _options);
        var $selectId = $("#"+_options.selectId), $childSelectId = $("#"+_options.childSelectId),
            $threeSelectId = $("#"+_options.threeSelectId), _formData = {};
        //第一级下拉change事件绑定
        $selectId.off('change').on('change',function(){
            var val = $.trim($(this).children('option:selected').val());
                val = val === 0 ? val+'' : val;
            var text = $.trim($(this).children('option:selected').text());
            if(val && val.length>0){
                _formData[_options.propertyName] = val;
                var _data = $.extend(_options.childData, _formData);
                if($threeSelectId){
                    $threeSelectId.html($("<option></option>").val("").text("请选择")).selectpicker('refresh');
                }
                $childSelectId.html($("<option></option>").val("").text("请选择"));
                that.comboBox(_options.childSelectId, _options.childUrl, _data, _options.childKeyName, _options.childValueName);
            }else {
                $childSelectId.html($("<option></option>").val("").text("请选择")).selectpicker('refresh');
                if($threeSelectId){
                    $threeSelectId.html($("<option></option>").val("").text("请选择")).selectpicker('refresh');
                }
            }
            if(_options.callbacksFunc){
                _options.callbacksFunc(val, text);
            }
        });
        //初始化下拉（第一级）
        that.comboBox(_options.selectId, _options.url, _options.data, _options.keyName, _options.valueName, _options.defSelectVal);
        //初始化下拉（第二级）
        that.comboBox(_options.childSelectId, _options.childUrl, {provinceCode: 0}, _options.childKeyName, _options.childValueName);

        //第二级下拉change事件绑定
        $childSelectId.off('change').on('change',function(){
            var val = $.trim($(this).children('option:selected').val());
            var text = $.trim($(this).children('option:selected').text());
            val = val === 0 ? val+'' : val;
            if(_options.threeSelectId && $threeSelectId){
                if(val && val.length>0){
                    _formData[_options.threeName] = val;
                    var _data = $.extend(_options.threeData, _formData);
                    $threeSelectId.html($("<option></option>").val("").text("请选择"));
                    that.comboBox(_options.threeSelectId, _options.threeUrl, _data, _options.threeKeyName, _options.threeValueName);
                }else {
                    $threeSelectId.html($("<option></option>").val("").text("请选择")).selectpicker('refresh');
                }
            }
            if(_options.childCallbacksFunc){
                _options.childCallbacksFunc(val, text);
            }
        });

        //第三级下拉change事件绑定
        if(_options.threeSelectId && $threeSelectId) {
            //初始化下拉（第三级）
            that.comboBox(_options.threeSelectId, _options.threeUrl, {cityCode: 0}, _options.threeKeyName, _options.threeValueName);
            $threeSelectId.off('change').on('change', function () {
                var val = $.trim($(this).children('option:selected').val());
                var text = $.trim($(this).children('option:selected').text());
                if (_options.threeCallbacksFunc) {
                    _options.threeCallbacksFunc(val, text);
                }
            });
        }
        //加载默认选中
        if(_options.defSelectVal && _options.childDefSelectVal){
            _formData[_options.propertyName] = _options.defSelectVal;
            var _data = $.extend(_options.childData, _formData);
            that.comboBox(_options.childSelectId, _options.childUrl, _data, _options.childKeyName, _options.childValueName, _options.childDefSelectVal);
            if(_options.threeDefSelectVal){
                _formData[_options.threeName] = _options.childDefSelectVal;
                var _data2 = $.extend(_options.threeData, _formData);
                that.comboBox(_options.threeSelectId, _options.threeUrl, _data2, _options.threeKeyName, _options.threeValueName, _options.threeDefSelectVal);
            }
        }
    }

});